package com.plugin.sproutcore.lang;

import java.util.ArrayList;
import java.util.List;

public class UnixExecutionEnvironment implements IExecutionEnvironment 
{
	private IShell shell;

	public List<String> generateCommandLine(List<String> command) 
	{
		// Need to flatten the commands so that /bin/sh -c can successfully run it.
		StringBuilder unixCommand = new StringBuilder();
		for (String param:command) 
		{
			unixCommand.append(param);
			unixCommand.append(" ");
		}
		command = new ArrayList<String>();
		command.add(unixCommand.toString());
		return command;
	}
	
	public IShell generateShell() 
	{
		if (shell == null) {
			shell = new UnixShell();
		}
		return shell;
	}
	
	/**
	 * Unix shell (sh). 
	 * More accurately 'sh -c'
	 */
	private static class UnixShell implements IShell
	{
		private static List<String> shellCommand = null;
		
		public List<String> getShellCommand() 
		{
			if (shellCommand == null) 
			{
				shellCommand = new ArrayList<String>();
				shellCommand.add("/bin/sh");
				shellCommand.add("-c");
			}
			return shellCommand;
		}

	}

}
